package com.eschool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eschool.mapper.CourseMapper;
import com.eschool.mapper.ScoreMapper;
import com.eschool.mapper.UserMapper;
import com.eschool.pojo.*;
import com.eschool.service.CourseService;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    CourseMapper courseMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ScoreMapper scoreMapper;

    @Override
    public List<CourseDAO> getAllCourse() {
        return courseMapper.selectList(null);
    }

    @Override
    public int ChooseCourse(ChooseCourseDTO chooseCourseDTO) {
        QueryWrapper q1 = new QueryWrapper();
        QueryWrapper q2 = new QueryWrapper();

        q1.eq("stuid",chooseCourseDTO.getStuid());
        q2.eq("courseid",chooseCourseDTO.getCourseid());
        UserDAO userDAO = userMapper.selectOne(q1);
        CourseDAO courseDAO = courseMapper.selectOne(q2);
        if(Integer.parseInt(userDAO.getGrade())!=Integer.parseInt(courseDAO.getClassgrade())){ //年级不一样
            return -1;//选不了课
        }
        else{
            ScoreDAO scoreDAO = new ScoreDAO();
            scoreDAO.setStuid(chooseCourseDTO.getStuid());
            scoreDAO.setCourseid(chooseCourseDTO.getCourseid());
            scoreDAO.setGrade(userDAO.getGrade());
            scoreDAO.setCoursename(courseDAO.getCoursename());
            scoreDAO.setTeacher(courseDAO.getTeacher());
            scoreDAO.setScore(null);
            scoreDAO.setScoreid(ShortUniqueId.generate());
            System.out.println(ShortUniqueId.generate());

            int insertRow = scoreMapper.insert(scoreDAO);
            return insertRow;
        }
    }

    @Override
    public List<CourseDAO> getChoosenCourses(Integer stuid) {
        QueryWrapper  queryWrapper = new QueryWrapper();
        queryWrapper.eq("stuid",stuid);
        List<ScoreDAO>  scoreDAOS;
        scoreDAOS = scoreMapper.selectList(queryWrapper);

        // 1. 提取所有 courseid 并去重
        List<String> courseIds = scoreDAOS.stream()
                .map(ScoreDAO::getCourseid)
                .distinct() // 去重
                .collect(Collectors.toList());

        List<CourseDAO> courseDAOS = new ArrayList<>();

        if (!courseIds.isEmpty()) {
            // 2. 批量查询 course 表
            QueryWrapper<CourseDAO> courseQueryWrapper = new QueryWrapper<>();
            courseQueryWrapper.in("courseid", courseIds); // 使用 IN 子句
            courseDAOS = courseMapper.selectList(courseQueryWrapper);

            // 按 classtime 排序
            courseDAOS.sort((c1, c2) -> {
                String[] time1 = c1.getClasstime().split("-");
                String[] time2 = c2.getClasstime().split("-");

                // 比较周次（第一个字段）
                int week1 = Integer.parseInt(time1[0]);
                int week2 = Integer.parseInt(time2[0]);
                if (week1 != week2) {
                    return Integer.compare(week1, week2); // 周次升序
                }

                // 周次相同，比较开始节数（第二个字段）
                int start1 = Integer.parseInt(time1[1]);
                int start2 = Integer.parseInt(time2[1]);
                return Integer.compare(start1, start2); // 节数升序
            });
        }

        return courseDAOS;
    }

    @Override
    public int DeleteCourse(Integer stuid, Integer courseid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("stuid",stuid);
        queryWrapper.eq("courseid",courseid);
        int updateRow = scoreMapper.delete(queryWrapper);
        return  updateRow;
    }

    @Override
    public List<CourseDAO> getCourseByInfo(getCourseByInfoDTO getCourseByInfoDTO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(getCourseByInfoDTO.getCoursename())){
            queryWrapper.like("coursename",getCourseByInfoDTO.getCoursename());
        }
        if(StringUtils.isNotBlank(getCourseByInfoDTO.getTeacher())){
            queryWrapper.like("teacher",getCourseByInfoDTO.getTeacher());
        }
        if (StringUtils.isNotBlank(getCourseByInfoDTO.getRoom())){
            queryWrapper.like("room",getCourseByInfoDTO.getRoom());
        }
        if (StringUtils.isNotBlank(getCourseByInfoDTO.getClasstime())){
            queryWrapper.like("classtime",getCourseByInfoDTO.getClasstime());
        }
        if (StringUtils.isNotBlank(getCourseByInfoDTO.getClassgrade())){
            queryWrapper.like("classgrade",getCourseByInfoDTO.getClassgrade());
        }
        List<CourseDAO> courseDAOS = courseMapper.selectList(queryWrapper);
        return courseDAOS;
    }

    @Override
    public List<ScoreDAO> getScoresByInfo(ScoreDTO scoreDTO) {
        QueryWrapper  queryWrapper = new QueryWrapper();
        queryWrapper.eq("stuid",scoreDTO.getStuid());
        if(StringUtils.isNotBlank(scoreDTO.getCoursename())){
            queryWrapper.like("coursename",scoreDTO.getCoursename());
        }
        if(StringUtils.isNotBlank(scoreDTO.getTeacher())){
            queryWrapper.like("teacher",scoreDTO.getTeacher());
        }
        if(StringUtils.isNotBlank(scoreDTO.getGrade())){
            queryWrapper.like("grade",scoreDTO.getGrade());
        }
        List<ScoreDAO> scoreDAOS = scoreMapper.selectList(queryWrapper);
        return scoreDAOS;
    }
}
